package com.web.admin.web.base.log;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.join.UserJoinHandler;
import com.web.base.log.facade.AccusationFacade;
import com.web.base.log.message.AccusationRequest;
import com.web.base.log.message.AccusationResponse;
import com.web.base.user.facade.UserFacade;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 举报控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月20日
 */
@Controller("logAccusationController")
@Http(url = "/base/log")
public class AccusationController
{
	@Autowired
	private AccusationFacade accusationFacade;
	@Autowired
	private UserFacade userFacade;
	
	private static DictParser parser = DictParser.getInstance();
	
	public AccusationController()
	{
	}

	@Http(url = "/accusation_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		AccusationRequest accusationRequest = new AccusationRequest();
		Page<AccusationResponse> page = accusationFacade.listPage(accusationRequest, Const.PAGE_START, Const.PAGE_SIZE);
		UserJoinHandler.userJoin(page.getObjectList(), userFacade);
		return View.createBuilder().setView("/base/log/accusation_list")
						.put("model", accusationRequest)
						.put("map", parser.getStringMap())
						.put("page", page)
						.build();
	}

	@Http(url = "/accusation_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		AccusationRequest accusationRequest = request.getPostObject(AccusationRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		Page<AccusationResponse> page = accusationFacade.listPage(accusationRequest, start, Const.PAGE_SIZE);
		UserJoinHandler.userJoin(page.getObjectList(), userFacade);
		return View.createBuilder().setView("/base/log/accusation_list_grid")
						.put("page", page)
						.build();
    }

    @Http(url = "/accusation_get", method = HttpMethod.GET)
    public View get(HttpRequest request)
    {
        int id = request.getQueryInt("id", 0);
        AccusationResponse response = null;
        if (id > 0)
        {
            response = accusationFacade.get(id);
            UserJoinHandler.userJoin(response, userFacade);
        }
        return View.createBuilder().setView("/base/log/accusation_get")
                        .put("model", response == null ? new AccusationResponse() : response)
                        .put("map", parser.getStringMap())
                        .build();
    }

    @Http(url = "/accusation_save", method = HttpMethod.POST)
    public void save(HttpRequest request)
    {
        AccusationRequest accusationRequest = request.getPostObject(AccusationRequest.class);
        AccusationResponse response = null;
        if (accusationRequest.getId() == null)
        {
            ValidationUtil.valid(accusationRequest, SaveModel.class);
            response = accusationFacade.add(accusationRequest);
        }
        else
        {
            ValidationUtil.valid(accusationRequest, UpdateModel.class);
            response = accusationFacade.update(accusationRequest);
        }
    }

}
